package com.leetcode.tree.bst;

import com.leetcode.basic.TreeNode;

/**
 * @author Dennis Li
 * @date 2020/7/18 19:13
 */
public class BSTLowestCommonAncestor_235 {

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root.left != null && root.val < p.val && root.val < q.val) {
            return lowestCommonAncestor(root.left, p, q);
        }
        if (root.right != null && root.val > p.val && root.val > q.val) {
            return lowestCommonAncestor(root.right, p, q);
        }
        return root;
    }

}
